package util;

import models.Card;
import models.Deck;

import java.util.ArrayList;
import java.util.logging.Logger;

public class DeckImporterExporter {

    private static Logger logger = Logger.getLogger(DeckImporterExporter.class.getName());

    public enum State {HEAD, DECKNAME, DECKNOTES, Q, A, TAIL}

    public static Deck fromText(String user, String s) {
        String[] lines = s.split("\n");
        State state = State.HEAD;
        Deck deck = new Deck(user, "");
        ArrayList<Card> cards = new ArrayList<Card>();
        Card currentCard = null;
        for (String line : lines) {
            if (state.equals(State.HEAD)) {
                if (line.trim().equals("")) continue;
                if (ifHasSectionMarker(line, "*")) {
                    state = State.DECKNAME;
                }
            } else if (state.equals(State.DECKNAME)) {
                if (line.trim().equals("")) continue;
                deck.name = line.trim();
                state = State.DECKNOTES;

            } else if (state.equals(State.DECKNOTES)) {
                if (line.trim().equals("")) continue;
                if (ifHasSectionMarker(line, "=")) {
                    state = State.Q;
                    currentCard = new Card(deck, "", "");
                    cards.add(currentCard);
                    continue;
                }
                deck.notes = deck.notes + line + "\n";
            } else if (state.equals(State.Q)) {
                if (ifHasSectionMarker(line, "-")) {
                    state = State.A;
                    continue;
                } else if (ifHasSectionMarker(line, "*")) {
                    state = State.TAIL;
                    cards.remove(currentCard);
                    break;
                }
                currentCard.setQuestion(currentCard.getQuestion() + line + "\n");
            } else if (state.equals(State.A)) {
                if (ifHasSectionMarker(line, "=")) { // finished parsing answer. New state: question
                    state = State.Q;
                    currentCard = new Card(deck, "", "");
                    cards.add(currentCard);
                    continue;
                }
                currentCard.setAnswer(currentCard.getAnswer() + line + "\n");
            }
        }

        // save data
        deck.insert();
        logger.info("Saved imported deck. Deck.id=" + deck.id);
        for (Card card : cards) {
            card.setDeck(deck);
            card.insert();
        }
        return deck;
    }

    private static boolean ifHasSectionMarker(String line, String m) {
        return line.trim().matches("\\" + m + "\\" + m + "\\" + m + "\\" + m + "*");
    }


    public static String toText(Deck deck) {
        StringBuffer sbX1 = new StringBuffer();
        sbX1.append("****************************************" + "\n");
        sbX1.append(deck.name + "\n");
        sbX1.append(deck.notes + "\n");
        sbX1.append("========================================" + "\n");
        for (Card card : deck.getCards()) {

            sbX1.append(card.getQuestion() + "\n");
            sbX1.append("----------------------------------------" + "\n");
            sbX1.append(card.getAnswer() + "\n");
            sbX1.append("========================================" + "\n");

        }

        sbX1.append("****************************************" + "\n");
        StringBuffer sbX = sbX1;
        return sbX.toString();
    }
}
